import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/constans/dict_data_info.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/basic_information/hazard_facility_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_entities_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/get_dict_all_entity.dart';

class EquipmentListController extends GetxController {
  /// 筛选右侧栏控制Key
  final popRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 搜索使用key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 搜索字符串
  String? search;

  /// 请求参数
  Map<String, dynamic> queryParameters = {};

  /// 筛选列表
  List<FilterBean> filterList = [];

  /// 设备类型
  String? equipTypeDicId;

  /// 设备等级
  String? equipLevel;

  /// 设备状态
  String? equipStatus;

  @override
  void onInit() {
    super.onInit();
    initData();
  }

  /// 初始化数据
  Future<void> initData() async {
    // 获取后端设备类型字典
    final List<GetDictAllEntity> dictTypeList = await DictDataInfo.getDictionaryDataByTree(dictionaryType: 'zdwxysblx_Tolcc9hz');
    // 获取后端设备等级字典
    final List<GetDictAllEntity> dictRankList = await DictDataInfo.getDictionaryDataByTree(dictionaryType: 'sbdj_jffgKw6U');

    final List<FilterEntitiesBean> typeList = [];

    // 添加类型筛选条件
    for (var item in dictTypeList) {
      typeList.add(FilterEntitiesBean(name: item.dictionaryLabel!, value: item.id!));
    }

    final List<FilterEntitiesBean> rankList = [];

    // 添加等级筛选条件
    for (var item in dictRankList) {
      rankList.add(FilterEntitiesBean(name: item.dictionaryLabel!, value: item.id!));
    }

    // 添加设备状态
    final List<FilterEntitiesBean> statusList = [];
    statusList.add(FilterEntitiesBean(name: '否', value: '0'));
    statusList.add(FilterEntitiesBean(name: '是', value: '1'));

    //筛选控制bean
    final FilterBean filterBean = FilterBean(title: '类型', filterList: typeList);
    final FilterBean filterBean2 = FilterBean(title: '等级', filterList: rankList);
    final FilterBean filterBean3 = FilterBean(title: '是否投用', filterList: statusList);
    filterList.add(filterBean);
    filterList.add(filterBean2);
    filterList.add(filterBean3);
  }

  Future<List<HazardFacilityEntity>> getListData(int pageNum, int pageSize) async {
    queryParameters['pageNum'] = pageNum;
    queryParameters['pageSize'] = pageSize;
    queryParameters['equipName'] = search;
    queryParameters['equipTypeDicId'] = equipTypeDicId;
    queryParameters['equipLevel'] = equipLevel;
    queryParameters['equipStatus'] = equipStatus;

    // 获取列表接口
    final res = await HttpUtil()
        .generateGetRequest<List<HazardFacilityEntity>>(
          Interface.getFacilityList,
          queryParameters: queryParameters,
        )
        .execute();
    List<HazardFacilityEntity>? data;
    res.onSuccess((result) {
      if (result.data != null) {
        data = result.data!;
      }
    });
    return data ?? [];
  } // 获取列表数据

  /// 筛选
  void screen() {
    popRightScaffoldKey.currentState?.openEndDrawer();
  }

  /// 筛选确认
  void screenConfirm(List<FilterBean> value) {
    filterList = value;
    equipTypeDicId = null;
    equipLevel = null;
    for (var i in value) {
      if (i.chosenValue.isEmpty) {
        continue;
      }
      if (i.title == '等级') {
        equipLevel = i.chosenValue[0].value as String;
      }
      if (i.title == '类型') {
        equipTypeDicId = i.chosenValue[0].value as String;
      }
      if (i.title == '是否投用') {
        equipStatus = i.chosenValue[0].value as String;
      }
    }
    update();
    refreshPage();
  }

  /// 搜索
  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }
}
